"use strict";

angular.module('app.yksensor').controller('SensorCtrl', function ($scope,sensorHttpS) {

    $scope.currentCompany = {id:1};
    //配置分页基本参数
    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 10,
        pagesLength: 5,
        totalItems: 20,
        perPageOptions: [10, 20, 30, 40, 50],

        onChange: function () {
        }
    };
    // 得到所有的设备
    var GetData = function () {
        sensorHttpS.postAll('getSensorPagi', $scope.paginationConf).then(function (response) {
            console.log(response);
            $scope.paginationConf.totalItems = response['sum'];
            $scope.sensorInfo = response['content'];
        });
    }

    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', GetData);
    // 所有的协议下拉菜单
    var getPro = function(){
        sensorHttpS.getAll("getProtocol").then(function(response){
            $scope.allProtocol = response;
        });
    }
    getPro();
    $scope.sensor = {};
    $scope.addSensor = function () {
        $('#myModal').modal('hide');
        var dataobj = {};
        dataobj.oper = "add";   
        dataobj.data = $scope.sensor;
        console.log(dataobj);
        sensorHttpS.postAll('editSensor', dataobj).then(function (response) {
            console.log(response);
            if (response > 0) {
                GetData();
                $.smallBox({
                    title: "添加传感器成功",
                    content: "<i class='fa fa-clock-o'></i> <i>添加传感器成功</i>",
                    color: "#659265",
                    iconSmall: "fa fa-check fa-2x fadeInRight animated",
                    timeout: 4000
                });
            }
            else {

            }

        })

    };

    $scope.editSensor = {};
    $scope.showEditSensor = function (item) {
        $scope.editSensor.des = item.des;
        $scope.editSensor.name = item.name;
        $scope.editSensor.device_num = item.device_num;
        $scope.editSensor.protocol_id = item.protocol_id;
        $scope.editSensor.cycle = Number(item.cycle);
        $('#myModalEdit').modal('show');

    };
          
    $scope.editSensorF = function () {
        $('#myModalEdit').modal('hide');
        var dataobj = {};
        dataobj.oper = "edit";   
        dataobj.data = $scope.editSensor;
        console.log(dataobj);
        sensorHttpS.postAll('editSensor', dataobj).then(function (response) {
            if (response > 0) {
                GetData();
                $.smallBox({
                    title: "编辑传感器成功",
                    content: "<i class='fa fa-clock-o'></i> <i>编辑传感器成功</i>",
                    color: "#659265",
                    iconSmall: "fa fa-check fa-2x fadeInRight animated",
                    timeout: 4000
                });
            }
            else {

            }

        })

    }

    $scope.delSensor = {};
    $scope.showDelDialog = function (device_num) {

        $scope.delSensor.id = device_num ;

        $('#showDelDialog').modal('show');

    }

    $scope.delSensorF = function () {

        $('#showDelDialog').modal('hide');
        var dataobj = {};
        var arr = [];
        arr.push($scope.delSensor.id);
        dataobj.oper = "del";   
        dataobj.data = arr;
        console.log(dataobj);
        sensorHttpS.postAll('editSensor',dataobj).then(function (response) {
            if (response > 0) {
                GetData();
                $.smallBox({
                    title: "删除传感器成功",
                    content: "<i class='fa fa-clock-o'></i> <i>删除传感器成功</i>",
                    color: "#659265",
                    iconSmall: "fa fa-check fa-2x fadeInRight animated",
                    timeout: 4000
                });
            }

        })

    }
      
    $scope.x=false;//默认未选中
    var choseArr=[];//定义数组用于存放前端选中的数据
    // 全选
    $scope.checkAll = function(v){
        // $("input[name = checkbox]").attr("checked",true);
        $("input[name = checkbox]").each(function(){
              $(this).prop("checked",true);              
        });
        choseArr = [];
        for(var i = 0; i < v.length; i++){
            var id = v[i].device_num;
            choseArr.push(id);
        }
        console.log(choseArr);
    }
    // 反选
    $scope.reserveCheck = function(v){
         choseArr = [];
         $("[name = checkbox]:checkbox").each(function (n) {
            $(this).prop("checked", !$(this).prop("checked"));
            if($(this).prop("checked") == true){
               var id = v[n].device_num;
               choseArr.push(id);
            }
        });
        console.log(choseArr);
    }
    // 单选改变样式
    $scope.checked = function(item,x){
        // item为数据，x为是否选中true，false
        var id = item.device_num;
        if (x == true) {//选中
           choseArr.push(id);
        }else{//取消选中
           delete_arr(id);
        }
        console.log(choseArr);
    }
    // 删除数组中指定的对象
    var delete_arr = function(id){
         for(var i = 0; i < choseArr.length; i++){
            var objid = choseArr[i];
            if(objid == id){
                choseArr.splice(i, 1);
                break;
            }
        }
    }
    //
    $scope.delectCheckShow = function(){
        if(choseArr.length > 0){
            $('#showDelDialog2').modal('show');
        }else{
            alert("请选择至少一条数据再操作");
        }
        
    }
   // 多选删除
     $scope.delectCheck = function(){
        $('#showDelDialog2').modal('hide');
        var dataobj = {};
        dataobj.oper = "del";
        dataobj.data = choseArr;
        console.log(dataobj);
         sensorHttpS.postAll('editSensor', dataobj).then(function (response) {
            if (response > 0) {
                GetData();
                choseArr = [];
                $.smallBox({
                    title: "删除传感器成功",
                    content: "<i class='fa fa-clock-o'></i> <i>删除传感器成功</i>",
                    color: "#659265",
                    iconSmall: "fa fa-check fa-2x fadeInRight animated",
                    timeout: 4000
                });

            }
            else {

            }

        })
        
     }
   $scope.submitAllShow = function(){
        if(choseArr.length > 0){
            $('#showDelDialog3').modal('show');
        }else{
            alert("请选择至少一条数据再操作");
        }
   }
   // 确认修改多个协议
   $scope.submitAll = function(){
       $('#showDelDialog3').modal('hide');
       // for(var i =0 ;i < choseArr.length;i++){
    	  //  choseArr[i].protocol_id = $("#select_value").val();
       // }
       var dataobj = {};
       dataobj.oper = "edit_more";
       dataobj.data = choseArr;
       dataobj.protocol_id = $("#select_value").val();
       console.log(dataobj);
       sensorHttpS.postAll('editSensor', dataobj).then(function (response) {
                console.log(response);
                if (response > 0) {               
                GetData();  
                choseArr = [];
                $.smallBox({
                title: "编辑传感器成功",
                content: "<i class='fa fa-clock-o'></i> <i>编辑传感器成功</i>",
                color: "#659265",
                iconSmall: "fa fa-check fa-2x fadeInRight animated",
                timeout: 4000  
              });    
            }
            else {

            }

    })                                         
   }
 
});
